﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Group3Game
{
    public partial class HighScoreForm : Form
    {
        public System.Windows.Forms.Label Label1
        { get { return label1; } }

        public System.Windows.Forms.Button Button1
        { get { return button1; } }

        public System.Windows.Forms.TextBox TextBox1
        { get { return textBox1; } }

        private string scorefile;

        public string HighScoreFile
        {
            get { return scorefile; }
            set { scorefile = value; }
        }
   
        public HighScoreForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GameForm.XnaThread.Abort();
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Tillåt inte att "kryssa ned" WinFormen.
            e.Cancel = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox1.Text != null) {
                if (!File.Exists(scorefile)) {
                    using (StreamWriter sw = File.CreateText(scorefile)) {
                        sw.WriteLine("<table border=\"1\">");
                        sw.WriteLine("<tr><td>Namn</td>" + "<td>Po&#228;ng</td>" + "<td>Level</td>" + "<td>Tid</td>" + "</tr>");
                        sw.WriteLine("<tr><td>" + textBox1.Text +"</td>" + "<td>" + Game1.Score + "</td>" + "<td>" + Game1.Level + "</td>" + "<td>" + Game1.Tid + "</td>" +"</tr>");
                        sw.WriteLine("</table>");
                        sw.WriteLine("</div>");
                        sw.WriteLine("</body>");
                        sw.WriteLine("</html>");
                    }
                }
                else {

                    List<string> scores = File.ReadAllLines(scorefile).ToList();

                    var oldscores = (from line in scores 
                                     where line != "<table border=\"1\">" && line != "</table>" select line).ToList();

                    List<string> newscores = new List<string>();
                    newscores.Add("<table border=\"1\">");

                    foreach (string o in oldscores) {
                        newscores.Add(o);
                    }

                    newscores.Add("<tr><td>" + textBox1.Text + "</td>" + "<td>" + Game1.Score + "</td>" + "<td>" + Game1.Level + "</td>" + "<td>" + Game1.Tid + "</td>" + "</tr>");
                    newscores.Add("</table>");
                
                    using (StreamWriter sw = new StreamWriter(scorefile))
                    {
                        foreach (string line in newscores)
                        {
                            sw.WriteLine(line);    
                        }
                    }
                }

                GameForm.XnaThread.Abort();
                Application.Exit();
            
            } else {
                System.Windows.Forms.MessageBox.Show("Du måste ange ett namn om du vill spara poäng!");
            }
        }
    }
}
